# handler opener open
# 也是发送请求 但是可以携带更高级的请求头

import urllib.request

url = 'http://www.baidu.com'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}

# 请求对象的定制只能使用url headers data 但是更高级的请求头就不可以使用了
request = urllib.request.Request(url=url, headers=headers)

handler = urllib.request.HTTPHandler()
opener = urllib.request.build_opener(handler)
# 相当于urllib.request.urlopen(request)
response = opener.open(request)

content = response.read().decode('utf-8')
print(content)
